//
//  DiscoverViewController.m
//  Wildfire Chat
//
//  Created by WF Chat on 2017/10/28.
//  Copyright © 2017年 WildFireChat. All rights reserved.
//

#import "DiscoverViewController.h"
#import "ChatroomListViewController.h"
#import "DeviceTableViewController.h"
#import <WFChatUIKit/WFChatUIKit.h>
#import <WFChatClient/WFCCIMService.h>
#import "DiscoverMomentsTableViewCell.h"
#ifdef WFC_MOMENTS
#import <WFMomentClient/WFMomentClient.h>
#import <WFMomentUIKit/WFMomentUIKit.h>
#endif
#if WFCU_SUPPORT_VOIP
#import <WFAVEngineKit/WFAVEngineKit.h>
#endif
#import "UIFont+YH.h"
#import "UIColor+YH.h"
#import "WFCConfig.h"
#import "YGExtendChain.h"
#import "SDAutoLayout.h"

#import "AFNetworking.h"
#import <WFChatClient/WFCChatClient.h>

 


@interface DiscoverViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, assign)BOOL hasMoments;
@property (nonatomic, strong)NSMutableArray *dataSource;
@property (nonatomic, assign)int count;

@end

@implementation DiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray arrayWithArray:@[
//                       @{@"title":LocalizedString(@"Chatroom"),@"image":@"discover_chatroom",@"des":@"chatroom"},
//                       @{@"title":LocalizedString(@"Robot"),@"image":@"fx_sach2",@"des":@"robot"},
//                       @{@"title":LocalizedString(@"Channel"), @"image":@"fx_sach",@"des":@"channel"},
                       
                       @{@"title":LocalizedString(@"动态"), @"image":@"fx_sach2",@"des":@"dynamic"},
                       @{@"title":LocalizedString(@"扫一扫"), @"image":@"fx_sach",@"des":@"scancode"},

                       
//                       @{@"title":LocalizedString(@"DevDocs"), @"image":@"dev_docs",@"des":@"Dev"}
//                     @{@"title":@"Things", @"image":@"discover_things",@"des":@"Things"}
    ]];
    
    if(NSClassFromString(@"SDTimeLineTableViewController")) {
        [self.dataSource insertObject:@{@"title":LocalizedString(@"Moments"),@"image":@"AlbumReflashIcon",@"des":@"moment"} atIndex:0];
        self.hasMoments = YES;
    } else {
        self.hasMoments = NO;
    }
#if WFCU_SUPPORT_VOIP
    if ([WFAVEngineKit sharedEngineKit].supportConference) {
        [self.dataSource addObject:@{@"title":LocalizedString(@"Conference"),@"image":@"discover_conference",@"des":@"Conference"}];
    }
#endif
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    if (@available(iOS 15, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    }
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.01)];
    [self.tableView reloadData];
    self.tableView.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
#ifdef WFC_MOMENTS
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUnreadCommentStatusChanged:) name:kReceiveComments object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUnreadCommentStatusChanged:) name:kClearUnreadComments object:nil];
    
#endif
    [self getNoticeList];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNoticeList) name:@"postNoticeList" object:nil];

    
}

- (void)onUnreadCommentStatusChanged:(NSNotification *)notification {
    [self.tableView reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self updateUnreadStatus];
    [self getNoticeList];
}

- (void)updateUnreadStatus {
    [self.tableView reloadData];
#ifdef WFC_MOMENTS
    int momentIndex = 2;
    if(WORK_PLATFORM_URL.length)
        momentIndex = 3;
    [self.tabBarController.tabBar showBadgeOnItemIndex:momentIndex badgeValue:[[WFMomentService sharedService] getUnreadCount]];
#endif
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 9)];
    view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 9;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;///<.统一行高>
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *des = self.dataSource[indexPath.section][@"des"];
    if ([des isEqualToString:@"dynamic"]) {
        NSLog(@"----------");
        UIViewController *vc = [[NSClassFromString(@"WFCUFCircleVC") alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if ([des isEqualToString:@"chatroom"]) {
        ChatroomListViewController *vc = [[ChatroomListViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
                  [self.navigationController pushViewController:vc animated:YES];
    }
    
//    if ([des isEqualToString:@"dynamic"]) {
//        ChatroomListViewController *vc = [[ChatroomListViewController alloc] init];
//        vc.hidesBottomBarWhenPushed = YES;
//                  [self.navigationController pushViewController:vc animated:YES];
//    }
    if ([des isEqualToString:@"scancode"]) {
        if (gQrCodeDelegate) {
            [gQrCodeDelegate scanQrCode:self.navigationController];
        }
    }
    
    
    
    if ([des isEqualToString:@"channel"]) {
        WFCUFavChannelTableViewController *channelVC = [[WFCUFavChannelTableViewController alloc] init];;
        channelVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:channelVC animated:YES];
    }
    
    if ([des isEqualToString:@"robot"]) {
            WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
            mvc.conversation = [[WFCCConversation alloc] init];
            mvc.conversation.type = Single_Type;
            mvc.conversation.target = @"FireRobot";
            mvc.conversation.line = 0;
        
            mvc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:mvc animated:YES];
        
    }
    

    if ([des isEqualToString:@"Dev"]) {
        WFCUBrowserViewController *vc = [[WFCUBrowserViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.url = @"http://docs.wildfirechat.cn";
        [self.navigationController pushViewController:vc animated:YES];
    }
//
//    if ([des isEqualToString:@"Things"]) {
//        DeviceTableViewController *vc = [[DeviceTableViewController alloc] init];
//        vc.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:vc animated:YES];
//    }
    
    if ([des isEqualToString:@"Conference"]) {
        WFZHomeViewController *vc = [[WFZHomeViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
     
    
    UITableViewCell *cell;
    if (indexPath.section == 0 && self.hasMoments) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"momentsCell"];
        if (cell == nil) {
            cell = [[DiscoverMomentsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"momentsCell"];
        }
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"defaultCell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"defaultCell"];
        }
    }
    

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:16];
    
    [cell.contentView removeAllSubviews];
    
    UIImageView *_portraitView = [UIImageView new];
    _portraitView.layer.masksToBounds = YES;
    _portraitView.layer.cornerRadius = 3.f;
    [cell.contentView addSubview:_portraitView];
    
    UILabel *_nameLabel = [UILabel new];
    _nameLabel.textColor = [UIColor blackColor];
    [cell.contentView addSubview:_nameLabel];
    
    _portraitView.sd_layout.centerYEqualToView(cell.contentView).leftSpaceToView(cell.contentView, 16).heightIs(48).widthIs(48);

    _nameLabel.sd_layout.centerYEqualToView(cell.contentView).leftSpaceToView(_portraitView, 16).autoHeightRatio(0);
    [_nameLabel setSingleLineAutoResizeWithMaxWidth:150];
    _nameLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:16];
    
    
    _nameLabel.text = self.dataSource[indexPath.section][@"title"];
    
    _portraitView.image = [UIImage imageNamed:self.dataSource[indexPath.section][@"image"]];
    
    
    
    if ([_nameLabel.text containsString:@"动态"] && self.count > 0) {
         
        
        UIButton *btn = [[UIButton alloc]init];
 
        [cell.contentView addSubview:btn];
        btn.sd_layout.rightSpaceToView(cell.contentView, 20).centerYEqualToView(cell.contentView).heightIs(20);
        btn.sh_chain_extend.backgroundColorRed().font_size(12).titleColorWhite().roundSize(10).title([NSString stringWithFormat:@"%ld",self.count ]);
        [btn setupAutoSizeWithHorizontalPadding:6 buttonHeight:20];
        
        
        
    }
    
    
    
    cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"right_arrow"]];
    
    if (indexPath.section == 0 && self.hasMoments) {
            DiscoverMomentsTableViewCell *momentsCell = (DiscoverMomentsTableViewCell *)cell;
            __weak typeof(self)ws = self;
#ifdef WFC_MOMENTS
            int unread = [[WFMomentService sharedService] getUnreadCount];
            if (unread) {
                momentsCell.bubbleView.hidden = NO;
                [momentsCell.bubbleView setBubbleTipNumber:unread];
            } else {
                momentsCell.bubbleView.hidden = YES;
            }
            NSMutableArray<WFMFeed *> *feeds = [[WFMomentService sharedService] restoreCache:nil];
            if (feeds.count > 0) {
                momentsCell.lastFeed = [feeds objectAtIndex:0];
            } else {
                [[WFMomentService sharedService] getFeeds:0 count:10 fromUser:nil success:^(NSArray<WFMFeed *> * _Nonnull feeds) {
                    if (feeds.count) {
                        [[WFMomentService sharedService] storeCache:feeds forUser:nil];
                        [ws.tableView reloadData];
                    }
                } error:^(int error_code) {
                    
                }];
            }
#endif
        }
    return cell;
}



- (void)getNoticeList{
    
    WEAKSELF_ZM
    
    NSString *savedToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"WFC_APPSERVER_AUTH_TOKEN"];
    NSString *userId = [WFCCNetworkService sharedInstance].userId;
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    
 
    [manager.requestSerializer setValue:savedToken forHTTPHeaderField:@"authToken"];

//    NSString *url = [NSString stringWithFormat:@"https://d.yehuoim.com/friend-circle/topic/userTopic/%@" , userId];
    NSString *url = [NSString stringWithFormat:@"https://d.yehuoim.com/friend-circle/topic/noticeListCount" ];

    
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
            
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dic = responseObject;
            
            NSLog(@" ret---  %@ ", dic );
            NSDictionary *data = [dic handleNullObjectForKey:@"data"];
            weakSelf.count = [[data handleNullObjectForKey:@"count"] intValue];
            
            [weakSelf.tabBarController.tabBar showBadgeOnItemIndex:2 badgeValue:[[data handleNullObjectForKey:@"count"] intValue] ];

            
        }
        
        [weakSelf.tableView reloadData];

        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        if (error) {
             
            NSLog(@" ret---  %@ ", error );

        }
 
    }];
}

//- (void)updateBadgeNumber {
//    int count = [[WFCCIMService sharedWFCIMService] getUnreadFriendRequestStatus];
//    [self.tabBarController.tabBar showBadgeOnItemIndex:1 badgeValue:count];
//}

@end
